package com.xxxx.server.controller;


import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.Result;
import com.xxxx.server.pojo.Role;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding Yi
 * @since 2021-07-07
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "获取所有操作员")
    @GetMapping("/")
    public Result getAllAdmins(String keywords) {
        List<Admin> adminList = adminService.getAllAdmins(keywords);
        return Result.success("hahah",adminList);
    }

    @ApiOperation(value = "更新操作员")
    @PutMapping("/")
    public Result updateAdmin(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/")
    public Result deleteAdmin(@RequestParam Integer id) {
        System.out.println("8888888888888888888888888888888");
        if (adminService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @ApiOperation(value = "获取所有角色")
    @GetMapping("/roles")
    public Result getAllRoles() {
        List<Role> roleList = roleService.list();
        return Result.success("获取成功",roleList);
    }

    @ApiOperation(value = "更新操作员角色")
    @PutMapping("/role")
    public Result updataAdminRoles(Integer adminId, Integer[] rids) {
        return adminService.updateAdminRole(adminId, rids);
    }

}
